package api

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
	"server/app/api/middleware"
	"server/app/api/route"
	"server/base"
	_ "server/docs" //依赖项必须导入
)

type Router struct{}

func (r Router) Init(g *gin.Engine) *gin.Engine {
	// 所有接口加上跨域中间件
	g.Use(middleware.Cors())

	// 如果开启了swagger 则启动swagger
	if base.App.Config.Http.Swagger {
		g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	}

	g.StaticFS("/static", http.Dir("./static"))

	api := g.Group("/api")
	{
		// API接口路由公共头部
		route.CommonRoute{}.InitRoute(api)

		// websocket
		route.WebsocketRoute{}.InitRoute(api)

		// user相关接口
		route.UserRoute{}.InitRoute(api)
	}

	return g
}
